package com.framework.modules.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.framework.common.utils.DateUtils;
import com.framework.modules.base.entity.PostsTrashEntity;
import com.framework.modules.base.entity.UserDoingEntity;
import com.framework.modules.base.service.*;
import com.framework.modules.dataview.entity.ViewUserPostsListEntity;
import com.framework.modules.dataview.service.ViewUserPostsListService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;

import com.framework.modules.base.dao.PostsDao;
import com.framework.modules.base.entity.PostsEntity;


@Service("postsService")
public class PostsServiceImpl extends ServiceImpl<PostsDao, PostsEntity> implements PostsService {
    @Autowired
    private PostsTrashService postsTrashService;
    @Autowired
    private UserDoingService userDoingService;

    @Autowired
    private ViewUserPostsListService viewUserPostsListService;

    @Autowired
    private PostsAttrService postsAttrService;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        List<String> descParaList=new ArrayList<String>();
        descParaList.add("adddate");
        Page<PostsEntity> page = this.selectPage(
                new Query<PostsEntity>(params).getPage(),
                new EntityWrapper<PostsEntity>().where("uid={0}",params.get("uid")).orderDesc(descParaList)
        );

        return new PageUtils(page);
    }

    @Override
    public Object postItemAdd(Long userid,PostsEntity entity) {
        if(userid==null||entity==null){
            return "参数有误";
        }
        entity.setUid(String.valueOf(userid));
        int date=DateUtils.getSecondTimestamp(new Date());
        entity.setState("1");
        entity.setAdddate(date);
        entity.setLastEdit(date);
        return baseMapper.insert(entity);
    }

    @Override
    public Object postItemMod(Long userid,PostsEntity entity) {
        if(userid==null||entity==null){
            return "参数有误";
        }
        PostsEntity postsEntity=baseMapper.selectById(entity.getId());
        if(postsEntity==null){
            return "没有此条动态";
        }
        if(StringUtils.isNotBlank(entity.getViewPolicy())){
            postsEntity.setViewPolicy(entity.getViewPolicy());
        }
        entity.setUid(String.valueOf(userid));
        int date=DateUtils.getSecondTimestamp(new Date());
        entity.setLastEdit(date);
        return baseMapper.updateById(entity);
    }

    @Override
    public Object postItemDelete(Long userid,Integer pid){
        if(userid==null||pid==null){
            return "参数有误";
        }
        PostsEntity entity=new PostsEntity();
        entity.setId(pid);
        entity.setUid(String.valueOf(userid));
        PostsEntity postsEntity=baseMapper.selectOne(entity);
        if(postsEntity==null){
            return "动态不存在";
        }
        JSONObject object=(JSONObject)JSON.toJSON(postsEntity);
        PostsTrashEntity postsTrashEntity=JSON.toJavaObject(object, PostsTrashEntity.class);
        if(postsTrashService.addPostsTrash(postsTrashEntity)==1){
            return baseMapper.deleteById(pid);
        }
        return "删除失败";
    }

    @Override
    public PageUtils getSpacePost(Map<String, Object> map) {
        Page<ViewUserPostsListEntity> page=new Page<>((Integer)map.get("page"),Integer.valueOf(map.get("limit").toString()));
        //1、联结查询用户表，查出用户头像和昵称，用户表字段 uid,nickname
        return new PageUtils(page.setRecords(viewUserPostsListService.getViewUserPostsList(page,map)));
    }

    @Override
    public Integer updatePostsLikeCount(Integer id,Integer likeCount,String type) {
        if("add".equals(type)){
            return this.baseMapper.addPostsLikeCount(id);
        }else if("reduce".equals(type)){
            return this.baseMapper.reducePostsLikeCount(id);
        }else{
            return this.baseMapper.updatePostslcsCount(id,likeCount,null,null);
        }
    }

    @Override
    public Integer updatePostsShareCount(Integer id, Integer count, String type) {
        if("add".equals(type)){
            return this.baseMapper.addPostsShareCount(id);
        }else if("reduce".equals(type)){
            return this.baseMapper.reducePostsShareCount(id);
        }else{
            return this.baseMapper.updatePostslcsCount(id,null,null,count);
        }
    }

    @Override
    public Integer updatePostsCommentCount(Integer id, Integer count, String type) {
        if("add".equals(type)){
            return this.baseMapper.addPostsCommentCount(id);
        }else if("reduce".equals(type)){
            return this.baseMapper.reducePostsCommentCount(id);
        }else{
            return this.baseMapper.updatePostslcsCount(id,null,count,null);
        }
    }

    @Override
    public Object likePosts(UserDoingEntity entity) {
        Integer objectId=entity.getObjectId();
        Integer uid=Integer.valueOf(entity.getUid());
        boolean isExist=userDoingService.isExist(entity);
        //判断是否关注，喜欢过
        if(isExist){
            //如果存在，就删除喜欢
            userDoingService.deleteDoing(entity);
            this.baseMapper.reducePostsLikeCount(objectId);
            postsAttrService.reducelikeUids(objectId,uid.toString());
        }else{
            //如果不存在，就添加喜欢
            entity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
            userDoingService.addDoing(entity);
            this.baseMapper.addPostsLikeCount(objectId);
            postsAttrService.addlikeUids(objectId,uid.toString());
        }
        return !isExist;
    }


}
